package com.dlc.felear.xiaoensale.utils;

import android.util.Log;


import com.dlc.felear.xiaoensale.constant.Constant;
import com.itdlc.android.library.utils.FileUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

public class RecordLog {
    private static final String TAG = "RecordLog";
    private static RecordLog instance;
    private ArrayBlockingQueue<String> mQueue;
    private FileWriter filerWriter;
    private BufferedWriter bufWriter;
    private ScheduledExecutorService scheduledThreadPool;

    private RecordLog() {
        FileUtil.createFilePath(Constant.LOG_PATH);
        File file = new File(Constant.LOG_PATH, TimeUtils.getCurrrentTimeYMD() + ".txt");
        scheduledThreadPool = new ScheduledThreadPoolExecutor(2);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            filerWriter = new FileWriter(file, true);
            bufWriter = new BufferedWriter(filerWriter);
        } catch (Exception e) {
            e.printStackTrace();
        }


        mQueue = new ArrayBlockingQueue<String>(1000);
        pollPicInfo();
    }

    public static RecordLog getInstance() {
        if (instance == null) {
            synchronized (RecordLog.class) {
                if (instance == null) {
                    instance = new RecordLog();
                }
            }
        }
        return instance;
    }

    public void saveLog(final String msg) {
        scheduledThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    mQueue.put(msg + "    " + TimeUtils.getCurrrentTimeHMS());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void pollPicInfo() {
        scheduledThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                String msg;
                try {
                    while ((msg = mQueue.take()) != null) {
                        saveMsgToFile(msg);
                        Thread.sleep(100);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void saveMsgToFile(String msg) {
        Log.e(TAG, "记录信息：" + msg);
        try {
            bufWriter.write(msg);
            bufWriter.write("\r\n");
            bufWriter.newLine();
            bufWriter.flush();
            filerWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void closeIo() {
        try {
            bufWriter.close();
            filerWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
